using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Data.SqlClient;
using Entities;
/// <summary>
/// Summary description for HangXeDAL
/// </summary>
namespace DataAccess
{

    public class HangXeDAL : BaseDAL
    {
        #region Private Variables

        #endregion

        #region Public Constructors
        public HangXeDAL()
        {
            //
            // TODO: Add constructor logic here
            //

        }
        #endregion



        #region Public Methods
        public int Insert(HangXe objHangXe)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "sp_HangXe_Insert";
            SqlParameter Sqlparam;

            Sqlparam = new SqlParameter("@MaHangXe", SqlDbType.NChar);
            Sqlparam.Value = objHangXe.MaHangXe;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@TenHangXe", SqlDbType.NVarChar);
            Sqlparam.Value = objHangXe.TenHangXe;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@ID", SqlDbType.Int);
            Sqlparam.Direction = ParameterDirection.ReturnValue;
            Sqlcomm.Parameters.Add(Sqlparam);


            int result = base.ExecuteNoneQuery(Sqlcomm);

            if (!Convert.IsDBNull(Sqlcomm.Parameters["@ID"]))
                result = Convert.ToInt32(Sqlcomm.Parameters["@ID"].Value);

            return result;
        }

        public int Update(HangXe objHangXe)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "sp_HangXe_UpdateByPK";
            SqlParameter Sqlparam;

            Sqlparam = new SqlParameter("@Khoa", SqlDbType.BigInt);
            Sqlparam.Value = objHangXe.Khoa;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@MaHangXe", SqlDbType.NChar);
            Sqlparam.Value = objHangXe.MaHangXe;
            Sqlcomm.Parameters.Add(Sqlparam);

            Sqlparam = new SqlParameter("@TenHangXe", SqlDbType.NVarChar);
            Sqlparam.Value = objHangXe.TenHangXe;
            Sqlcomm.Parameters.Add(Sqlparam);



            Sqlparam = new SqlParameter("@ErrorCode", SqlDbType.Int);
            Sqlparam.Direction = ParameterDirection.ReturnValue;
            Sqlcomm.Parameters.Add(Sqlparam);

            int result = base.ExecuteNoneQuery(Sqlcomm);

            if (!Convert.IsDBNull(Sqlcomm.Parameters["@ErrorCode"]))
                result = Convert.ToInt32(Sqlcomm.Parameters["@ErrorCode"].Value);

            return result;


        }

        public int Delete(HangXe objHangXe)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "sp_HangXe_DeleteByPK";
            SqlParameter Sqlparam;


            Sqlparam = new SqlParameter("@Khoa", SqlDbType.BigInt);
            Sqlparam.Value = objHangXe.Khoa;
            Sqlcomm.Parameters.Add(Sqlparam);



            int result = base.ExecuteNoneQuery(Sqlcomm);
            return result;
        }

        public int DeleteAll()
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "sp_HangXe_DeleteAll";

            int result = base.ExecuteNoneQuery(Sqlcomm);
            return result;
        }

        public HangXe Select(HangXe objHangXe)
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "sp_HangXe_GetByPK";
            SqlParameter Sqlparam;


            Sqlparam = new SqlParameter("@Khoa", SqlDbType.BigInt);
            Sqlparam.Value = objHangXe.Khoa;
            Sqlcomm.Parameters.Add(Sqlparam);



            DataSet ds = base.GetDataSet(Sqlcomm);
            DataRow dr = null;
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                dr = ds.Tables[0].Rows[0];
                if (!Convert.IsDBNull(dr["Khoa"]))
                    objHangXe.Khoa = Convert.ToInt64(dr["Khoa"]);
                if (!Convert.IsDBNull(dr["MaHangXe"]))
                    objHangXe.MaHangXe = Convert.ToString(dr["MaHangXe"]);
                if (!Convert.IsDBNull(dr["TenHangXe"]))
                    objHangXe.TenHangXe = Convert.ToString(dr["TenHangXe"]);

            }
            return objHangXe;
        }

        public ArrayList SelectAllToArray()
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "sp_HangXe_GetAll";

            DataSet ds = base.GetDataSet(Sqlcomm);
            DataTable dt = null;
            ArrayList arrHangXe = new ArrayList();
            if (ds != null && ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];
                foreach (DataRow dr in dt.Rows)
                {
                    HangXe objHangXe = new HangXe();
                    if (!Convert.IsDBNull(dr["Khoa"]))
                        objHangXe.Khoa = Convert.ToInt64(dr["Khoa"]);
                    if (!Convert.IsDBNull(dr["MaHangXe"]))
                        objHangXe.MaHangXe = Convert.ToString(dr["MaHangXe"]);
                    if (!Convert.IsDBNull(dr["TenHangXe"]))
                        objHangXe.TenHangXe = Convert.ToString(dr["TenHangXe"]);
                    arrHangXe.Add(objHangXe);
                }
            }
            return arrHangXe;
        }

        public DataTable SelectAll()
        {

            SqlCommand Sqlcomm = new SqlCommand();
            Sqlcomm.CommandType = CommandType.StoredProcedure;
            Sqlcomm.CommandText = "sp_HangXe_GetAll";

            DataSet ds = base.GetDataSet(Sqlcomm);
            DataTable dt = null;

            if (ds != null && ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];

            }
            return dt;
        }


        #endregion

    }

}
